package org.jz.gae.redbook.spb.server.controller;

import java.math.BigDecimal;
import java.math.RoundingMode;

import org.jz.gae.redbook.spb.server.helper.LocationHelper;
import org.jz.gae.redbook.spb.shared.LocationEntityView;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.GeoPt;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

public class LocationUtils
{
	public static void reflectEntityToView(
			Entity location,
			LocationEntityView newLocation)
	{
		Key districtKey = (Key)location.getProperty(LocationHelper.LOCATION_DISTRICT_KEY);
		Key speciesKey = (Key)location.getProperty(LocationHelper.LOCATION_SPECIES_KEY);
		newLocation.setLocationKey(KeyFactory.keyToString(location.getKey()));
		if (districtKey != null && (!"".equals(districtKey)))
		{
		    newLocation.setLocationDistrictKey(KeyFactory.keyToString(districtKey));
		}
		if (speciesKey != null && (!"".equals(speciesKey)))
		{
		    newLocation.setLocationSpeciesKey(KeyFactory.keyToString(speciesKey));
		}
		newLocation.setLocationInitialData((String)location.getProperty(LocationHelper.LOCATION_INITIAL_DATA));
		newLocation.setLocationWorkName((String)location.getProperty(LocationHelper.LOCATION_WORK_NAME));
		newLocation.setLocationDescription((String)location.getProperty(LocationHelper.LOCATION_DESCRIPTION));
		newLocation.setLocationCenose((String)location.getProperty(LocationHelper.LOCATION_CENOSE));
		newLocation.setLocationCoordLat(new BigDecimal((Double)location.getProperty(LocationHelper.LOCATION_COORD_LAT)).setScale(5, RoundingMode.HALF_UP));
		newLocation.setLocationCoordLong(new BigDecimal((Double)location.getProperty(LocationHelper.LOCATION_COORD_LONG)).setScale(5, RoundingMode.HALF_UP));
		newLocation.setLocationSpecificExistingThreats((String)location.getProperty(LocationHelper.LOCATION_SPECIFIC_EXISTING_THREATS));
		newLocation.setLocationSpecificPotentialThreats((String)location.getProperty(LocationHelper.LOCATION_SPECIFIC_POTENTIAL_THREATS));
		newLocation.setLocationNote((String)location.getProperty(LocationHelper.LOCATION_NOTE));
		/**/
		GeoPt point = (GeoPt)location.getProperty(LocationHelper.LOCATION_COORDS);
		if (point != null)
		{
		    newLocation.setLocationCoordLatitude(point.getLatitude());
		    newLocation.setLocationCoordLongitude(point.getLongitude());
		}
	}
	
    public static void reflectViewToEntity(
			LocationEntityView _LocationToEdit,
			Entity location)
	{
		location.setProperty(LocationHelper.LOCATION_WORK_NAME, _LocationToEdit.getLocationWorkName());
        location.setProperty(LocationHelper.LOCATION_DISTRICT_KEY, KeyFactory.stringToKey(_LocationToEdit.getLocationDistrictKey()));
        location.setProperty(LocationHelper.LOCATION_SPECIES_KEY, KeyFactory.stringToKey(_LocationToEdit.getLocationSpeciesKey()));
		location.setProperty(LocationHelper.LOCATION_INITIAL_DATA, _LocationToEdit.getLocationInitialData());
		location.setProperty(LocationHelper.LOCATION_DESCRIPTION, _LocationToEdit.getLocationDescription());
		location.setProperty(LocationHelper.LOCATION_CENOSE, _LocationToEdit.getLocationCenose());
		location.setProperty(LocationHelper.LOCATION_COORDS, new GeoPt((float)_LocationToEdit.getLocationCoordLatitude(), (float)_LocationToEdit.getLocationCoordLongitude()));
		location.setProperty(LocationHelper.LOCATION_COORD_LAT, _LocationToEdit.getLocationCoordLat().doubleValue());
		location.setProperty(LocationHelper.LOCATION_COORD_LONG, _LocationToEdit.getLocationCoordLong().doubleValue());
		location.setProperty(LocationHelper.LOCATION_SPECIFIC_EXISTING_THREATS, _LocationToEdit.getLocationSpecificExistingThreats());
		location.setProperty(LocationHelper.LOCATION_SPECIFIC_POTENTIAL_THREATS, _LocationToEdit.getLocationSpecificPotentialThreats());
		location.setProperty(LocationHelper.LOCATION_NOTE, _LocationToEdit.getLocationNote());
	}


}
